<?php
class AttrValueFormShowModel extends Model{
	public $table = 'stock';
	public function showStock($gid){
		// $stockData = $this->where(array('gid'=>$gid))->all();
		$pre = C('DB_PREFIX');
		$sql = 'SELECT * FROM '.$pre.'stock AS s JOIN '.$pre.'goods_pic AS gp ON s.pic_id=gp.pic_id WHERE s.gid = '.$gid;
		$stockData = M()->query($sql);
		if(empty($stockData))return;
		foreach ($stockData as $key => $value) {
			$stid = $value['stid'];
			$stockAttrData = M('stock_attr')->where(array('stid'=>$stid))->all();
			// p($stockAttrData);exit;


			foreach ($stockAttrData as $n => $d) {

				$stockData[$key]['html'][$n]['av_id'] = $d['av_id'];
				$av_id = $d['av_id'];
				//获得属性值表中的acid 和属性值 并放入数组
				$attr_value_data = M('attr_value')->where(array('av_id'=>$av_id))->all();
				$stockData[$key]['html'][$n]['attr_value'] = $attr_value_data[0]['attr_value'];
				$stockData[$key]['html'][$n]['ac_id'] = $attr_value_data[0]['ac_id'];

				//获得商品类型表中的 属性名
				$attr_class = M('attr_class')->where(array('ac_id'=>$attr_value_data[0]['ac_id']))->all();
				$stockData[$key]['html'][$n]['attr_name'] = $attr_class[0]['attr_name'];
				$stockData[$key]['html'][$n]['show_type'] = $attr_class[0]['show_type'];

			}
			// $Data = $stockData[$key]['html'];
			// $model = K('AttrFormShow');
			// $model->attrForm($gtid,$Data);
		}
		
		//生成web显示列表框
		foreach ($stockData as $key => $value) {
			$stid = $value['stid'];
			//没有数据时返回
			foreach ($value['html'] as $n => $d) {
				$function = $d['show_type'];
				$av_id = $d['av_id'];
				$ac_id = $d['ac_id'];
				$web = $this->$function($ac_id,$av_id,$stid);

				$stockData[$key]['html'][$n]['web'] = $web;
			}
		}
		return $stockData;
	}

	public function dropdown($acid,$av_id=null,$stid){
		$attrValue = M('attr_value')->where(array('ac_id'=>$acid))->all();
		$name = $attrValue[0]['ac_id'];
		$html = "<select name='spec[gg][{$name}][]'>";
		
		foreach ($attrValue as $n => $v) {
			$selected = '';
			if($v['av_id']==$av_id){
				$selected = " selected=''";
			}
			$html.="<option".$selected." value='".$v['av_id']."|".$v['attr_value']."|".$stid."'>".$v['attr_value']."</option>";
		}
		$html.= "</select>";
		return $html;
	}
	
	
}
?>
